
//var  myURL = 'https://xtcee.xyz:8080/appDevice/postApi/' //// url: 'https://127.0.0.1:8080/appDevice/postApi/', 

var baseMyUrl = 'https://123.249.33.130:8084/' //
var Domain = 'https://xtcee.xyz' //域名
//var baseMyUrl = Domain+':8084/'
var myURL_login_post = baseMyUrl+'appDevice/postLogin/'
var myURL_getDevice_get = baseMyUrl+'appDevice/getGetDevice/'
var myURL_getDevice_post = baseMyUrl+'appDevice/postGetDevice/'
var  myURL_setDevice_post = baseMyUrl+'appDevice/postSetDevice/'
var myURL_addDevice_post = baseMyUrl+'appDevice/postAddDevice/'
var myURL_getAddDeviceNum_post = baseMyUrl+'appDevice/postGetAddDeviceNum/'

var header = {
  'Content-type': 'application/json' // 默认值
}
/*
// 由台灯向服务器发送状态信息，包括了心跳

两部分：主题和负载
主题：用户名加设备编号
负载：
message ILampStateHeart {
  int64 user_ID = 1;
  DEVICE_TYPE DeviceType = 2;
  int32 DeviceID = 3;
  DEVICE_SWITCH_STATE DeviceSwitchState = 4;
  int32 Luminance = 5;
  int32 Color  = 6;
}
*/

var DEVICE_TYPE = {
  ILmap : 0,
  Robor : 1,
}
var DEVICE_SWITCH_STATE = {
  ON : 0,
  OFF :1,
}

var DATATYPE = {
  type_Switch : 0,
  type_Luminance :1,
  type_Color: 2,
}

var DataType = DATATYPE.type_Switch //数据类型
var Topic = 1381234432110
var Payload = {
  USER_ID : Topic,
  Device_ID : 1381234432110,
  Device_Type : DEVICE_TYPE.ILmap,
  Device_Num:1,
  DeviceSwitchState : DEVICE_SWITCH_STATE.ON,
  Luminance: 1000,
  Color: 50
}

var loginFlag = {
  login_success :1 , //#登录成功
  login_wrong_userNumber :2 , //# 账号错误
  login_wrong_password :3, //# 密码错误
  login_wrong_other :4//
}

function complete_ (aaa) {
  console.log("aaaa"+aaa)
  return aaa
}

async function post_request(pTopic, Payload) {
  var aaaa = 123
  //s使用POST请求发送亮度数据
  wx.request({
    url: myURL, 
    method:'POST', // 请求方式POST
    data: {
      Topic   :pTopic, //由用户设置的账号密码
      payload : Payload, //负载 要发送的具体数据内容
    },
    header: {
      'Content-type': 'application/json' // 默认值
    },
    success (res) {
      //aaaa = res.data[0].Topic
      return res.data[0].Topic
    },
    fail :(err) => {
      console.log('request'+err)
      return null
    },
    complete: function( res ) { 
      //console.log("post_request+"+ res.data[0], res.data[0].Topic)
    }
  })
}



function ShowToast(Title, time, Icon) {
  //console.log(Title)
  wx.showToast({
    title: Title,
    duration: time,
    icon: Icon // success loading
  })
}


function buttonGetTest() {
  console.log("发送了一个Get请求");
  wx.request({
    //url: 'https://xtcee.xyz:8080/api/getApi', //仅为示例，并非真实的接口地址
    url: 'https://127.0.0.1:8080/api/getApi', //仅为示例，并非真实的接口地址
    header: {
      'content-type': 'application/json' // 默认值
    },
    success (res) {
      console.log(res.data)
    }
  })
}

function  buttonPostTest() {
  console.log("发送了一个Post请求");
  wx.request({
    //url: 'https://xtcee.xyz:8080/api/postApi', //仅为示例，并非真实的接口地址
    url: 'https://127.0.0.1:8080/api/postApi/', 
    method:'POST', // 请求方式POST
    data: {
      UserID  : "1234567890",//由微信小程序和web进行自动分配
        DeviceID: "1234567890", //由微信小程序和web进行自动分配
        PassWord: "12345678", //由用户设置的账号密码
        Topic   : "test", //由用户设置的账号密码
      payload : "|L=1|", //负载 要发送的具体数据内容
    },
    header: {
      'Content-type': 'application/json' // 默认值
    },
    success (res) {
      console.log(res.data)
    },
    complete: function( res ) { 
      console.log(res.data)
    }
  })

}


function request(pTopic, Payload) {
    return new Promise(function(resolve, reject) {
        wx.request({
            url: myURL_setDevice_post, 
            method:'POST', // 请求方式POST
            data: {
              Topic   :pTopic, //由用户设置的账号密码
              payload : Payload, //负载 要发送的具体数据内容
            },
            header: {
              'Content-type': 'application/json' // 默认值
            },
            success(res) {
                console.log(res);
                //请求成功返回数据
                resolve(res.data);
            },
            fail(err) {
                //请求失败
                reject(err)
                console.log(err);
            }
        })
    })
}


const http = (options) =>{
  return new Promise((resolve,reject) => {
    wx.request({
      url: myURL_setDevice_post, 
      method:'POST', // 请求方式POST
      data: {
        Topic   :1231232, //由用户设置的账号密码
        //payload : Payload, //负载 要发送的具体数据内容
      },
      header: {
        'Content-type': 'application/json' // 默认值
      },
      success:resolve,
      fail:reject
    })
  })
}

export {
  DEVICE_TYPE,
  DEVICE_SWITCH_STATE,
  Topic,
  Payload,
  ShowToast, 
  post_request,
  complete_,
  request,
  http,
  header,
  DATATYPE,
  DataType,
  loginFlag,
  myURL_login_post,
  myURL_getDevice_get,
  myURL_getDevice_post,
  myURL_addDevice_post, //
  myURL_getAddDeviceNum_post, // postGetAddDeviceNum
  myURL_setDevice_post,
};